package com.site.util.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.site.util.constant.Constants;

/**
 * 接入设备必须为手机接入过滤器
 * @author jack
 *
 */
public class AccessDeviceInterceptor extends AbstractInterceptor {
 
	private static final long serialVersionUID = 8313355115891426453L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
        HttpServletResponse response= (HttpServletResponse) actionContext.get(StrutsStatics.HTTP_RESPONSE);
        
        if (response != null && request.getHeader("User-Agent").equals(Constants.USER_AGENT)){
        	return invocation.invoke();
        } else {
        	response.setContentType("text/html;charset=utf-8");
        	response.getWriter().print("警告,您访问非法地址！");
        	return null;
        }
	}
}
